angular.module('Caltech').controller('userHistoryController', function ($scope, Restangular, ngTableParams, $stateParams, $location, Constants, $filter) {
    'use strict';
    $scope.users = [];

    $scope.tableParams = new ngTableParams({
        page: 1,
        count: 15
    }, {
        counts: [15, 20, 25],
        getData: function ($defer, params) {
            Restangular.all('userHistory')
                .getList({
                    page: params.page(),
                    count: params.count()
                }).then(function (quotes) {
                    params.total(quotes.total);
                    $defer.resolve(quotes);
                    $scope.users = quotes;
                });
        }
    });

    var today = new Date();

    $scope.toDate = new Date();

    $scope.fromDate = new Date(today.setDate(today.getDate() - 7));

    $scope.clearAll = function () {
        Restangular.all('userHistory')
            .customPOST({}, 'clearAll').then(function () {
                $scope.tableParams.reload();
            });
    };

    $scope.clear = function () {
        Restangular.all('userHistory')
           .customPOST({
               fromDate: $filter('date')($scope.fromDate, 'MM/dd/yyyy'),
               toDate: $filter('date')($scope.toDate, 'MM/dd/yyyy')
           }, 'clear').then(function () {
               $scope.tableParams.reload();
           });
    };

});